import { query2 } from "../../mysqlQuery.js";
import Joi from "joi";
import { getRandomId } from "../../common/index.js";
import SDTime from "silly-datetime"; //时间格式化处理插件
//新增菜单
function addMeun(req, res, next) {
  const schema = Joi.object().keys({
    meunType: Joi.number().required(),
    title: Joi.string().required(),
    hidden: Joi.number().required(),
    icon: Joi.string().required(),
    active: Joi.number().required(),
    path: Joi.string().required(),
    redirect: Joi.string().allow(""),
    user_id: Joi.string().required(),
    parent_id: Joi.string().allow(""),
    component: Joi.string().allow(""),
    parentMeunSort: Joi.string().required(),
    // childMeunSort: Joi.allow(""),
    cache: Joi.number().allow(""),
    name: Joi.string().allow(""),
    // parentName: Joi.string().allow(""),
  });
  const result = Joi.validate(req.body, schema, { allowUnknown: true });
  if (result.error != null) {
    next(result.error);
    return;
  }

  query2([
    {
      sql: `insert into meunlist (meunType,title,hidden,icon,active,path,redirect,user_id,parent_id,component,parentMeunSort,childMeunSort,cache,name,parentName,meun_id,create_time) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
      values: [
        req.body.meunType,
        req.body.title,
        req.body.hidden,
        req.body.icon,
        req.body.active,
        req.body.path,
        req.body.redirect,
        req.body.user_id,
        req.body.parent_id,
        req.body.component,
        req.body.parentMeunSort,
        req.body.childMeunSort,
        req.body.cache,
        req.body.name,
        req.body.parentName,
        getRandomId(8),
        SDTime.format(new Date(), "YYYY-MM-DD HH:mm:ss"),
      ],
    },
  ])
    .then((out) => {
      res.send({
        code: 200,
        msg: "新增成功!",
        data: out[0].rows,
      });
    })
    .catch((err) => {
      next(err);
    });
}
//删除菜单
function loseMeun(req, res, next) {
  const schema = Joi.object().keys({
    meun_id: Joi.string().required(),
  });
  const result = Joi.validate(req.query2, schema, { allowUnknown: true });
  if (result.error != null) {
    next(result.error);
    return;
  }
  let values = [],
    sql = "";
  values = req.query2.meun_id.split(",");
  values.forEach((element) => {
    sql += `delete from meunlist where meun_id=?;`;
  });
  query2([
    {
      sql,
      values,
    },
  ])
    .then((out) => {
      res.send({
        code: 200,
        msg: "删除成功！",
        data: out,
      });
    })
    .catch((err) => {
      next(err);
    });
}
//修改菜单信息
function putMeun(req, res, next) {
  let schema,
    sql = "",
    values = [];
  if (req.body.meunType == 0) {
    //一级分组
    schema = Joi.object().keys({
      title: Joi.string().required(),
      hidden: Joi.string().required(),
      icon: Joi.string().required(),
      name: Joi.string().required(),
      user_id: Joi.string().required(),
      meun_id: Joi.string().required(),
    });
    sql =
      "update meunlist set title=?,hidden=?,icon=?,name=?,user_id=?,create_time=? where meun_id=?;";
    values = [
      req.body.title,
      req.body.hidden,
      req.body.icon,
      req.body.name,
      req.body.user_id,
      SDTime.format(new Date(), "YYYY-MM-DD HH:mm:ss"),
      req.body.meun_id,
    ];
  } else if (req.body.meunType == 1) {
    //一级菜单
    schema = Joi.object().keys({
      icon: Joi.string().required(),
      path: Joi.string().required(),
      name: Joi.string().required(),
      component: Joi.string().required(),
      title: Joi.string().required(),
      cache: Joi.number().required(),
      hidden: Joi.number().required(),
      active: Joi.number().required(),
      user_id: Joi.string().required(),
      meun_id: Joi.string().required(),
    });
    sql =
      "update meunlist set icon=?,path=?,name=?,component=?,title=?,cache=?,hidden=?,active=?,user_id=?,create_time=? where meun_id=?;";
    values = [
      req.body.icon,
      req.body.path,
      req.body.name,
      req.body.component,
      req.body.title,
      req.body.cache,
      req.body.hidden,
      req.body.active,
      req.body.user_id,
      SDTime.format(new Date(), "YYYY-MM-DD HH:mm:ss"),
      req.body.meun_id,
    ];
  } else {
    //二级菜单
    schema = Joi.object().keys({
      icon: Joi.string().required(),
      path: Joi.string().required(),
      name: Joi.string().required(),
      parentName: Joi.string().required(),
      component: Joi.string().required(),
      title: Joi.string().required(),
      cache: Joi.number().required(),
      hidden: Joi.number().required(),
      active: Joi.number().required(),
      user_id: Joi.string().required(),
      meun_id: Joi.string().required(),
    });
    sql =
      "update meunlist set icon=?,path=?,name=?,parentName=?,component=?,title=?,cache=?,hidden=?,active=?,user_id=?,create_time=? where meun_id=?;";
    values = [
      req.body.icon,
      req.body.path,
      req.body.name,
      req.body.parentName,
      req.body.component,
      req.body.title,
      req.body.cache,
      req.body.hidden,
      req.body.active,
      req.body.user_id,
      SDTime.format(new Date(), "YYYY-MM-DD HH:mm:ss"),
      req.body.meun_id,
    ];
  }
  const result = Joi.validate(req.body, schema, { allowUnknown: true });
  if (result.error != null) {
    next(result.error);
    return;
  }
  query2([
    {
      sql,
      values,
    },
  ])
    .then((out) => {
      res.send({
        code: 200,
        msg: "修改成功！",
        data: out[0].rows,
      });
    })
    .catch((err) => {
      next(err);
    });
}
//查询菜单
function getMeun(req, res, next) {
  query2([
    {
      sql: "select * from meunlist",
    },
  ])
    .then((out) => {
      let oneMeun = out[0].rows.filter((item) => item.meunType != 2);
      oneMeun.forEach((item) => {
        item.cache = item.cache == 0 ? false : true;
        item.active = item.active == 0 ? false : true;
        item.hidden = item.hidden == 0 ? false : true;
        item.create_time=SDTime.format(item.create_time, "YYYY-MM-DD HH:mm:ss")
      });
      oneMeun.sort((a,b)=>{
        return a.parentMeunSort-b.parentMeunSort
      })
      let twoMeun = out[0].rows.filter((item) => item.meunType == 2);
      twoMeun.forEach((item) => {
        item.cache = item.cache == 0 ? false : true;
        item.active = item.active == 0 ? false : true;
        item.hidden = item.hidden == 0 ? false : true;
        item.create_time=SDTime.format(item.create_time, "YYYY-MM-DD HH:mm:ss")
      });
      twoMeun.sort((a,b)=>{
        return a.childMeunSort-b.childMeunSort
      })
      oneMeun.forEach((item) => {
        if (item.meunType == 0) {
          item.children = twoMeun.filter(
            (child) => child.parent_id == item.meun_id
          );
        }
      });
      res.send({
        code: 200,
        msg: "查询成功！",
        data: oneMeun,
      });
    })
    .catch((err) => {
      next(err);
    });
}
export { addMeun, loseMeun, putMeun, getMeun };
